package Controllers;

import Connection.FileUserManager;
import Connection.UserManager;
import Connection.WebUserManager;
import SwingComponents.UserLoginPanel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UserLoginController {

    private PrincipalController principalController;
    private UserLoginPanel userLoginPanel;
    
    public UserLoginController(PrincipalController principalController) {
        this.principalController = principalController;
    }
    
    public void execute() {
        userLoginPanel = new UserLoginPanel();
        principalController.getPrincipalPanel().setVisible(false);
        principalController.getApplication().add(userLoginPanel);
        userLoginPanel.setVisible(true);
        userLoginPanel.getLoginButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UserManager userManager = new WebUserManager(principalController.getApplication());
                if (userManager.logIn(userLoginPanel.getUserField().getText(), userLoginPanel.getPasswordField().getPassword().toString())){
                    principalController.getUser().setGameState(userManager.getGameState(userLoginPanel.getUserField().getText()));
                    principalController.getUser().setUsername(userLoginPanel.getUserField().getText());
                }
            }
        });
        userLoginPanel.getCancelButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                principalController.getApplication().remove(userLoginPanel);
                principalController.getPrincipalPanel().setVisible(true);
            }
        });
    }
}
